package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.EmployerBudget;
import cn.anxingongyou.anxin.entity.UserParent;
import cn.anxingongyou.anxin.entity.Wages;
import cn.anxingongyou.anxin.entity.WorkerBudget;
import cn.anxingongyou.anxin.service.EmployerBudgetService;
import cn.anxingongyou.anxin.service.UserParentService;
import cn.anxingongyou.anxin.service.WagesService;
import cn.anxingongyou.anxin.service.WorkerBudgetService;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import com.sun.prism.d3d.D3DTextureData;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class WagesController {

    private final WagesService wagesService;
    private final WorkerBudgetService workerBudgetService;
    private final EmployerBudgetService employerBudgetService;
    private final UserParentService userParentService;
    @Autowired
    public WagesController(WagesService wagesService, WorkerBudgetService workerBudgetService, EmployerBudgetService employerBudgetService, UserParentService userParentService) {
        this.wagesService = wagesService;
        this.workerBudgetService = workerBudgetService;
        this.employerBudgetService = employerBudgetService;
        this.userParentService = userParentService;
    }

    @ApiOperation("添加打卡记录信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/wages/add",method = RequestMethod.POST)
    public Map addWages(
            @RequestBody @ApiParam(value = "打卡记录信息", required = true) Wages wages
            ){
        wages.setCreated(new Date());
        wages.setUpdated(new Date());
        int res = wagesService.insert(wages);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除打卡记录信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/wages/del/{id}",method = RequestMethod.GET)
    public Map delWages(
            @PathVariable @ApiParam(value = "打卡记录主键", required = true) Long id
    ){
        int res = wagesService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新打卡记录信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/wages/update",method = RequestMethod.POST)
    public Map updateWages(
            @RequestBody @ApiParam(value = "打卡记录信息,必传主键", required = true) Wages wages
    ){
        wages.setUpdated(new Date());
        int res = wagesService.updateByPrimaryKeySelective(wages);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("api更新打卡记录信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/api/wages/update",method = RequestMethod.POST)
    public Map apiUpdateWages(
            @RequestBody @ApiParam(value = "打卡记录信息,必传主键", required = true) Wages wages
    ){
        wages.setUpdated(new Date());
        int res = wagesService.updateByPrimaryKeySelective(wages);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取单个打卡记录通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/wages/findByWages",method = RequestMethod.POST)
    public Map findOneWages(
            @RequestBody @ApiParam(value = "打卡记录信息", required = true) Wages wages
    ){
        Wages wages1 = wagesService.selectOne(wages);
        if(wages1 != null) {
            return Result.<Wages>instance(200,"获取成功",wages1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取打卡记录信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/wages/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "打卡记录主键", required = true) Long id
    ){
        Wages wages = wagesService.selectOneByPrimaryKey(id);
        if(wages != null) {
            return Result.<Wages>instance(200,"获取成功",wages).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api获取打卡记录信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/wages/findOneById/{id}",method = RequestMethod.GET)
    public Map apiFindOneById(
            @PathVariable @ApiParam(value = "打卡记录主键", required = true) Long id
    ){
        Wages wages = wagesService.selectOneByPrimaryKey(id);
        if(wages != null) {
            return Result.<Wages>instance(200,"获取成功",wages).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api微信支付成功后调用，用于修改用户信息并返回、添加账单")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/wages/pay/success/{id}",method = RequestMethod.GET)
    public Map apiWagesPaySuccess(
            @PathVariable @ApiParam(value = "打卡记录主键", required = true) Long id
    ){
        // 查询打卡记录
        Wages wages = wagesService.selectOneByPrimaryKey(id);

        // 添加求职者收入账单
        WorkerBudget workerBudget = new WorkerBudget();
        workerBudget.setUserPid(wages.getUserPid());
        workerBudget.setUserWid(wages.getUserWid());
        workerBudget.setBudgetType(0);
        workerBudget.setMoneyType(0);
        workerBudget.setMoney(wages.getWorkerGetPrice());
        workerBudget.setCreated(new Date());
        workerBudget.setUpdated(new Date());
        workerBudgetService.insert(workerBudget);

        // 添加招聘者支出账单
        EmployerBudget employerBudget = new EmployerBudget();
        employerBudget.setEmployerPid(wages.getEmployerPid());
        employerBudget.setEmployerWid(wages.getEmployerEid());
        employerBudget.setBudgetType(1);
        employerBudget.setMoneyType(0);
        employerBudget.setMoney(wages.getPaidPrice());
        employerBudget.setCreated(new Date());
        employerBudget.setUpdated(new Date());
        employerBudgetService.insert(employerBudget);

        // 求职者用户的钱包增加钱
        UserParent userParent = userParentService.selectOneByPrimaryKey(wages.getUserPid());
        UserParent userParent1 = new UserParent();
        userParent1.setId(wages.getUserPid());
        userParent1.setWorkerWalletMoney(userParent.getWorkerWalletMoney() + Float.parseFloat(wages.getWorkerGetPrice()));
        userParentService.updateByPrimaryKeySelective(userParent1);

        // 招聘者用户的支出钱包增加钱
        UserParent userParent2 = userParentService.selectOneByPrimaryKey(wages.getEmployerPid());
        userParent2.setEmployerWalletMoney(userParent2.getEmployerWalletMoney() + Float.parseFloat(wages.getPaidPrice()));
        userParent2.setUpdated(new Date());
        userParentService.updateByPrimaryKeySelective(userParent2);


        if(wages != null) {
            return Result.instance(200,"操作成功").toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取打卡记录求职者")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/wages/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByWages(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "打卡记录信息", required = true)  Wages wages){
        PageInfo<Wages> pageInfo = wagesService.page(pageNum, pageSize, wages);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api获取打卡记录求职者")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/wages/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map apiSelectWagesByWages(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "打卡记录信息", required = true)  Wages wages){
        PageInfo<Wages> pageInfo = wagesService.page(pageNum, pageSize, wages);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

}
